<template>
  <div id="">
    array
  </div>
</template>
<script>
export default {
  name: "",
  data: () => ({

  }),
  created() {
    let array = [
      { date: '201801', brand: '美的', value: 100, hh: 2 },
      { date: '201801', brand: '格力', value: 70, hh: 3 },
      { date: '201802', brand: '美的', value: 32, hh: 2 },
      { date: '201802', brand: '格力', value: 55, hh: 2 },
      { date: '201803', brand: '美的', value: 22, hh: 2 },
      { date: '201804', brand: '美的', value: 45, hh: 2 },
      { date: '201804', brand: '小米', value: 100, hh: 2 },
    ]

    /**
     * 补全数组
     * @param  {[Array]} array     [数组]
     * @param  {[Array<String>]} optionArr [维度数组，支持1~2个维度]
     * @param  {[String]} valueName [补充元素值的名称]
     * @param  {[String, Number]} value     [补充元素的值]
     * @return {[Array]}           [返回结果数组]
     */
    function supplyArray (array, optionArr, valueName, value) {
      let result = []
      if (array.length) {
        if (optionArr.length === 1) {
          const key = optionArr[0]
          let keyArr = array.map(item => item[key])
          keyArr = Array.from(new Set(keyArr))
        } else if (optionArr.length === 2) {
          const key1 = optionArr[0]
          const key2 = optionArr[1]
          const otherKey = Object.keys(array[0]).filter(item => [key1, key2, valueName].indexOf(item) < 0)
          let keyArr1 = array.map(item => item[key1])
          let keyArr2 = array.map(item => item[key2])
          keyArr1 = Array.from(new Set(keyArr1))
          keyArr2 = Array.from(new Set(keyArr2))
          // 重构result
          keyArr1.forEach(item => {
            keyArr2.forEach(jItem => {
              result.push({
                [key1]: item,
                [key2]: jItem,
                [valueName]: value
              })
            })
          })
          result.forEach(item => {
            array.forEach(jItem => {
              if (item[key1] === jItem[key1] && item[key2] === jItem[key2]) {
                item[valueName] = jItem[valueName]
                otherKey.forEach(key => {
                  item[key] = jItem[key]
                })
              }
            })
          })
        }
      }
      return result
    }

    let result = supplyArray(array, ['date', 'brand'], 'value', 0)
    console.log('array', array)
    console.log('result', result)
  }
}
</script>
<style lang="scss" scoped>
</style>
